//
//  AppDelegate.m
//  i0S007通用项目
//
//  Created by Apple on 16/2/16.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "AppDelegate.h"
#import "CZSplitViewController.h"
#import "CZTabBarController.h"
#import "CZMenuViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 1.创建window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 程序从storyboard加载控制器,默认加载的是UISplitViewController,使用在iPad上的面布局
    // 2.当运行在iPhone上面,需要显示UITabBarController
    if (isIPhone) {
        // 2.1创建CZTabBarController
        CZTabBarController *tabBarVC = [[CZTabBarController alloc] init];
        
        // 2.2.替换window的rootViewController
        self.window.rootViewController = tabBarVC;
    } else {
        // iPad界面
        // 2.3创建CZSplitViewController
        CZSplitViewController *splitVC = [[CZSplitViewController alloc] init];
        
        // 2.4.替换window的rootViewController
        self.window.rootViewController = splitVC;
        
        // 2.5.设置状态栏
        application.statusBarStyle = UIStatusBarStyleLightContent;
    }
    
    // 2.6.显示状态栏
    // 在项目配置文件配置了 Hide status bar 和 info.plist里面配置了View controller-based status bar appearance = NO,程序启动后就不会再显示状态栏了
    application.statusBarHidden = NO;
    
    // 3.让window成为主窗口并显示
    [self.window makeKeyAndVisible];
    
    return YES;
}

// 屏幕发生改变之前
// newStatusBarOrientation 旋转后的屏幕方向
// duration 旋转需要的时间
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration {

    // 1.获取到CZSplitViewController
    // 在这个地方进行强制类型转换,OC是动态语言,实际类型需要在运行的时候才能确定
    CZSplitViewController *splitVC = (CZSplitViewController *)self.window.rootViewController;
    
    // 2.在运行的时候判断一下,如果不是CZSplitViewController就直接返回
    if (![splitVC isKindOfClass:[CZSplitViewController class]]) {
        return;
    }
    
    // 3.设置master的宽度
    [splitVC setPrimaryColumnWidthOrientatin:newStatusBarOrientation];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
